package com.gs.montaditos;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class mainActivity extends Activity {
	SimpleAdapter adapter;
	List<HashMap> fillMaps;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        setContentView(R.layout.main);
        ListView lv= (ListView)findViewById(R.id.list);

        // create the grid item mapping
        String[] from = new String[] {"montaditoId", "name", "image"};
        int[] to = new int[] { R.id.number, R.id.text, R.id.image};

        // prepare the list of all records
        Montaditos.init(getApplicationContext());
        fillMaps = Montaditos.getAll();

        // fill in the grid_item layout
        
        adapter = new SimpleAdapter(this, (List<? extends Map<String, ?>>) fillMaps, R.layout.item, from, to);
        lv.setAdapter(adapter);
        
        ImageButton share = (ImageButton)findViewById(R.id.share);
        share.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		});
        
        ImageButton cart = (ImageButton)findViewById(R.id.cart);
        cart.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent i = new Intent(getApplicationContext(), ListOrder.class);
				i.putExtra("waiter", false);
				startActivity(i);
			}
		});
        
        ImageButton waiter = (ImageButton)findViewById(R.id.waiter);
        waiter.setOnClickListener(new View.OnClickListener(){
			
			@Override
			public void onClick(View v) {
				Intent i = new Intent(getApplicationContext(), ListOrder.class);
				i.putExtra("waiter", true);
				startActivity(i);
				
			}
		});
        
    }

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	
	private void setEvents(ListView lv){
		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> adapter, View view,
					int position, long id) {
				final Integer pos = position;
				final View v = view;
				final Integer selected = new Integer(((TextView) view
						.findViewById(R.id.amount)).getText().toString());
				AlertDialog.Builder dialog = new AlertDialog.Builder(
						mainActivity.this);
				dialog.setMessage(getString(R.string.list_text));
				dialog.setTitle("");
				dialog.setNegativeButton(R.string.remove,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								Order.remove(selected);
								HashMap tmp = (HashMap)mainActivity.this.adapter.getItem(pos);
								Integer a = new Integer(new Integer((String)tmp.get("amount")) - 1);
								if (a > 0) {
									tmp.put("amount", a);
								} else {
									Iterator it = fillMaps.iterator();
									while (it.hasNext()) {
										HashMap m = (HashMap) it.next();
										TextView id = (TextView)v.findViewById(R.id.montaditoId);
										
										if (m.get("montaditoId").equals(id.getText())) {
											fillMaps.remove(m);
											break;
										}
									}
								}
								mainActivity.this.adapter.notifyDataSetChanged(); 
								mainActivity.this.adapter.notifyDataSetInvalidated();
							}
						});
				dialog.setPositiveButton(R.string.add,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								Order.add(selected);
								HashMap tmp = (HashMap)mainActivity.this.adapter.getItem(pos);
								Integer a = new Integer(new Integer((String)tmp.get("amount")) + 1);
								tmp.put("amount", a);
								mainActivity.this.adapter.notifyDataSetChanged(); 
								mainActivity.this.adapter.notifyDataSetInvalidated();
							}
						});
				dialog.setNeutralButton(R.string.cancel,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
							}
						});
				dialog.show();
			}
		});
	}

}